package com.kaifamiao.www.demo.do0821;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.UnaryOperator;

public class Demo01 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "d", "b", "a", "c", "e", "f");
        //Collections.sort(list);
        sort(list);
        System.out.println(list);

        //int c = Collections.binarySearch(list,"c");
        int c = binarySearch1(list,"c");
        System.out.println(c);

        //返回指定集合中等于指定对象的元素数。
        System.out.println(Collections.frequency(list,"a"));

        //根据其元素的自然顺序，返回给定集合的最大元素。
        String max = Collections.max(list);
        System.out.println(max);
        list.replaceAll(new UnaryOperator<String>() {
            @Override
            public String apply(String s) {
                if (s.equals("a")){
                    return "A";
                }
                return s;
            }
        });
        //将列表中一个指定值的所有匹配项替换为另一个值。

        Collections.replaceAll(list,"b","B");

        System.out.println(list);
        //将指定列表中的元素旋转指定的距离。
        Collections.rotate(list,2);
        System.out.println(list);
        //使用默认的随机性源随机置换指定的列表。
        Collections.shuffle(list);
        System.out.println(list);
    }

    public static void sort(String[] strs) {
        if (strs == null){
            throw new NullPointerException();
        }
        if (strs.length < 2){
            return;
        }else{
            for (int i = 0; i < strs.length-1;i++){
                for (int j = 0;j < strs.length-1-i;j++){
                    if (strs[j].compareTo(strs[j+1])>0){
                        String temp = strs[j];
                        strs[j] = strs[j+1];
                        strs[j+1] = temp;
                    }
                }
            }
        }
    }
    public static void sort(List<String> list){
        if (list == null || list.size() < 2){
            return;
        }
        for (int i = 0;i < list.size() - 1;i++){
            for (int j = 0;j < list.size() - 1 - i;j++){
                if (list.get(j).compareTo(list.get(j+1))>0){
                    String temp = list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,temp);
                    //String temp = list.set(j,list.get(j+1))  list.set(j + 1,temp);
                }
            }
        }
    }
    public static int binarySearch(List<String> list,String str){
        if (list == null){
            throw new NullPointerException();
        }
        int high = list.size() - 1;
        int low = 0;
        while (low <= high){
            int mid = (low + high) >>>1;//  /2
            if (str.compareTo(list.get(mid))<0){
                high = mid - 1;
            }
            else if (str.compareTo(list.get(mid))>0){
                low = mid+1;
            }else{
                return mid;
            }
        }
        return - 1;
    }

    /**
     * 用递归方法重写binarySearch
     * @param list
     * @param str
     * @return
     */
    public static int binarySearch1(List<String> list,String str){
        if (list == null){
            throw new NullPointerException();
        }
        int high = list.size() -1;
        int low = 0;
        return binarySearch11(list,str,low,high);
    }

    public static int binarySearch11(List<String> list,String str,int low,int high){
        if (low >high){
            return -1;
        }
        int mid = (low + high) / 2;
        if (str.compareTo(list.get(mid))<0){
            return binarySearch11(list,str,low,mid-1);
            }else if (str.compareTo(list.get(mid))>0){
            return binarySearch11(list,str,mid+1,high);
        } else {
            return mid;
        }
    }
}
